#!/usr/local/php/bin/php
<?php

$data_source = 'done.txt';
$data_source = $argv[1];
//print_r($argv);
if (file_exists($data_source))
{
	$fp = fopen($data_source, 'r');
}
else
{
	die($data_source.' is not exist!\n');
}
if (!$fp)
{
	echo "Error: open {$data_source}";
	exit(0);
}
else
{
	$arr_data = array();
	$arr_ori_data = array();
	$info = "start time: ".date('Y-m-d H:i:s');
	while (!feof($fp))
	{
		$ori_data = fgets($fp);
		if (!empty($ori_data))	
		{
			$arr_ori_data = explode("\t", $ori_data);
		//	$key = $arr_ori_data[0].$arr_ori_data[1];
			//echo $key."\n";
	//		echo base64_decode($arr_ori_data[0])."\n";
			$arr_data[$arr_ori_data[0]] = $arr_ori_data[2];		

		}
	}
	fclose($fp);
	echo "count=".count($arr_data)."\n";
	natsort($arr_data);

	//array_multisort($sort_volume, SORT_NUMERIC, SORT_DESC, $arr_data); 
//	print_r($arr_data);
	$sum_data = file_get_contents("sum_".$data_source);
	$sum_data_arr = explode("\t", $sum_data);
	print_r($sum_data_arr);
	
	$f_name = "stat_".$data_source;
	$f = fopen($f_name, 'w');
	foreach ($arr_data AS $key => $val)
	{
		$percent = 1000000*$val/$sum_data_arr[0];
	//	echo $percent."\n";	
		fwrite($f, base64_decode($key)."\t".$percent."\n");
	}
	fclose($f);
	//file_put_contents("s_".$data_source, implode("\n", $arr_data));
	$info .= "\nend time: ".date('Y-m-d H:i:s');
	echo $info;
}	


?>
